package org.zzh.controller.cargo.extCproduct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.zzh.dao.common.PageBean;
import org.zzh.domain.cargo.extCproduct.CoExtCproduct;
import org.zzh.domain.cargo.factory.CoFactory;
import org.zzh.service.cargo.extCproduct.CoExtCproductService;
import org.zzh.service.cargo.factory.CoFactoryService;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class CoExtCproductController {

    @Autowired
    CoExtCproductService coExtCproductService;

    @Autowired
    CoFactoryService coFactoryService;

    @RequestMapping(value = "/list.do")
    public ModelAndView list(
            @RequestParam(required = false, value = "contractId") String contractId,
            @RequestParam(required = false, value = "contractProductId") String contractProductId,
            @RequestParam(required = false, value = "page", defaultValue = "1") Integer page,
            @RequestParam(required = false, value = "pageSize", defaultValue = "5") Integer pageSize
    ) {
        ModelAndView modelAndView = new ModelAndView();
        PageBean<CoExtCproduct> pageBean = coExtCproductService.list(contractId, contractProductId, page, pageSize);
        List<CoFactory> list = coFactoryService.list();
        modelAndView.addObject("factoryList", list);
        modelAndView.addObject("pageBean", pageBean);
        modelAndView.addObject("contractId", contractId);
        modelAndView.addObject("contractProductId", contractProductId);
        modelAndView.setViewName("/WEB-INF/pages/cargo/extc/extc-list");
        return modelAndView;
    }

    @RequestMapping(value = "/toUpdate.do")
    public ModelAndView toUpdate(
            @RequestParam(required = false, value = "id") String id,
            @RequestParam(required = false, value = "contractId") String contractId,
            @RequestParam(required = false, value = "contractProductId") String conntractProductId
    ) {
        ModelAndView modelAndView = new ModelAndView();
        List<CoFactory> list = coFactoryService.list();
        modelAndView.addObject("factoryList", list);
        modelAndView.addObject("extCproduct", coExtCproductService.findById(id));
        modelAndView.addObject("contractId", contractId);
        modelAndView.addObject("contractProductId", conntractProductId);
        modelAndView.setViewName("/WEB-INF/pages/cargo/extc/extc-update");
        return modelAndView;
    }

    @RequestMapping(value = "/delete.do")
    public String delete(
            @RequestParam(required = false, value = "id") String id,
            @RequestParam(required = false, value = "contractId") String contractId,
            @RequestParam(required = false, value = "contractProductId") String contractProductId
    ) {
        coExtCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId=" + contractId + "&contractProductId=" + contractProductId;
    }


    @RequestMapping(value = "edit.do")
    public String edit(@ModelAttribute CoExtCproduct extCproduct,
                             @RequestParam("productPhoto") MultipartFile productPhoto,
                             @RequestParam(required = false, value = "contractId") String contractId,
                             @RequestParam(required = false, value = "contractProductId") String contractProductId) {
        coExtCproductService.edit(extCproduct);
        return "redirect:/cargo/extCproduct/list.do?contractId=" + contractId + "&contractProductId=" + contractProductId;
    }
}
